package com.dkd.flow.strategy.service.operation;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSONObject;
import com.dkd.common.core.domain.entity.SysUser;
import com.dkd.flow.context.ProcessContext;
import com.dkd.flow.context.ProcessContextBuilder;
import com.dkd.flow.domain.EProcessInstances;
import com.dkd.flow.domain.EProcessTask;
import com.dkd.flow.emums.FlowOperation;
import com.dkd.flow.emums.FlowStatus;
import com.dkd.flow.emums.TaskActionType;
import com.dkd.flow.emums.TaskStatus;
import com.dkd.flow.service.impl.EProcessTaskServiceImpl;
import com.dkd.flow.strategy.annotation.Strategy;
import com.dkd.flow.utils.RevokeUtils;
import com.dkd.system.service.impl.SysUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Strategy(FlowOperation.REVOKE)
public class RevokeStrategy extends BaseStrategy {
    @Autowired
    SysUserServiceImpl sysUserServiceImpl;
    @Autowired
    ProcessContextBuilder processContextBuilder;
    @Autowired
    public RevokeUtils revokeUtils;
    @Autowired
    public EProcessTaskServiceImpl eProcessTaskServiceImpl;
    @Override
    public Long execute(JSONObject data) {
        // 使用构建器初始化上下文 --- start --- //
        ProcessContext context = processContextBuilder.build(data);
        EProcessInstances eProcessInstances = context.getEProcessInstances();
        EProcessTask curTask = context.getCurTask();
        Long curTaskId = context.getCurTaskId();
        Long curTaskStatus = context.getCurTaskStatus();
        EProcessInstances paramsEProcessInstances = context.getEProcessInstances();
        JSONObject formValue = context.getFormValue();
        Long processDefinitionId = context.getProcessDefinitionId();
        // 使用构建器初始化上下文 --- end --- //
        Long taskGroupId = taskCreate.createAndSetOperationGroupId(eProcessInstances);
        initNodeRelations(processDefinitionId);
        TaskStatus currentStatus = TaskStatus.getTaskStatusByValue(curTaskStatus.intValue());
        JSONObject launchNode = links.get(0).getJSONObject("toNode");
        if (launchNode == null) {
            throw new RuntimeException("links 中第一个节点的 toNode 为 null，无法创建发起人任务");
        }
        //创建	发起人任务 INITIATOR_TASK 1	下一个
        SysUser liableUser = nodeUtils.getLiableUser(eProcessInstances);
        switch (currentStatus) {
            //发起 INITIATE_APPLICATION 1
            //重新提交 RE_SUBMIT 12
            //重新提交所有 RE_SUBMIT_ALL 13
            case INITIATE_APPLICATION:
            case RE_SUBMIT:
            case RE_SUBMIT_ALL:
                //创建	发起人任务 INITIATOR_TASK 1	自己	已撤回 RECALL 10	草稿 DRAFT 0
                Long launchRecallTaskId = IdUtil.getSnowflakeNextId();
                taskCreate.createLaunchTask(eProcessInstances,
                                            launchNode,
                                            null,
                                            TaskStatus.RECALL,
                                            launchRecallTaskId,
                                            null,
                                            launchRecallTaskId,
                                            liableUser,
                                            taskGroupId,
                                            JSONObject.toJSONString(formValue),
                                            TaskActionType.OPERATION_TASK);
                //创建	发起人任务 INITIATOR_TASK 1	自己	草稿 DRAFT 11	草稿 DRAFT 0
                Long launchDraftTaskId = IdUtil.getSnowflakeNextId();
                taskCreate.createLaunchTask(eProcessInstances,
                                            launchNode,
                                            null,
                                            TaskStatus.DRAFT,
                                            launchDraftTaskId,
                                            null,
                                            launchRecallTaskId,
                                            liableUser,
                                            taskGroupId,
                                            JSONObject.toJSONString(formValue),
                                            TaskActionType.LOG_TASK);
                //创建	审批人任务 APPROVER_TASK 2	下一个	已取消 CANCELED 9	草稿 DRAFT 0
                revokeUtils.createSynTasksWithStatus(curTask,
                                                     eProcessInstances,
                                                     TaskStatus.CANCELED,
                                                     launchDraftTaskId,
                                                     taskGroupId,
                                                     formValue,
                                                     TaskActionType.LOG_TASK);
                //实例状态
                eProcessInstances.setFlowStatus(FlowStatus.DRAFT.getValue().longValue());
                updateEProcessInstances(eProcessInstances);
                //清除操作组ID
                taskCreate.clearOperationGroupId(eProcessInstances);
                break;
            //已审批 APPROVAL 3
            case REJECT_AND_END:
            case RETURNED_ALL:
            case RETURNED:
            case APPROVAL:
                //创建	审批人任务 APPROVER_TASK 2	自己	已撤回 RECALL 10	处理中 PROCESSING 1
                Long approvalRecallTaskId = IdUtil.getSnowflakeNextId();
                taskCreate.approverTask(eProcessInstances,
                                        nodeUtils.getApproverByTask(curTask),
                                        curTask.getProcessDesignId(),
                                        TaskStatus.RECALL,
                                        approvalRecallTaskId,
                                        approvalRecallTaskId,
                                        null,
                                        taskGroupId,
                                        JSONObject.toJSONString(formValue),
                                        TaskActionType.OPERATION_TASK);
                //撤回的时候，检查是否已经生成下一个节点任务
                if (revokeUtils.checkNextTaskGenerated(curTask.getTaskGroupId(),
                                                       curTaskId)) {
                    //创建	审批人任务 APPROVER_TASK 2	下一个	已取消 CANCELED 9	处理中 PROCESSING 1
                    revokeUtils.createFollowingTasksWithStatus(eProcessInstances,
                                                               curTask.getTaskGroupId(),
                                                               TaskStatus.CANCELED,
                                                               approvalRecallTaskId,
                                                               formValue,
                                                               TaskActionType.LOG_TASK);
                    //重置审批人数量，并减一个
                    resetEProcessInstancesApproverCount(eProcessInstances,
                                                        curTask.getProcessDesignId(),
                                                        nodeUtils.getRequiredApprovalCount(curTask.getProcessDesignId()));
                    nodeUtils.setEProcessInstancesApproverCount(eProcessInstances,
                                                                "-",
                                                                1);
                }
                else {
                    //如果没到下一个审批人，则在已审批完审批人数量的基础上要减一个审批人
                    if (currentStatus == TaskStatus.APPROVAL) {
                        nodeUtils.setEProcessInstancesApproverCount(eProcessInstances,
                                                                    "-",
                                                                    1);
                    }
                }
                //创建	审批人任务 APPROVER_TASK 2	自己	审批中 UNDER_APPROVAL 2	处理中 PROCESSING 1
                Long approveTaskId = IdUtil.getSnowflakeNextId();
                taskCreate.approverTask(eProcessInstances,
                                        nodeUtils.getApproverByTask(curTask),
                                        curTask.getProcessDesignId(),
                                        TaskStatus.UNDER_APPROVAL,
                                        approveTaskId,
                                        approvalRecallTaskId,
                                        null,
                                        taskGroupId,
                                        JSONObject.toJSONString(formValue),
                                        TaskActionType.LOG_TASK);
                //处理中 PROCESSING 1
                eProcessInstances.setFlowStatus(FlowStatus.PROCESSING.getValue().longValue());
                updateEProcessInstances(eProcessInstances);
                //清除操作组ID
                taskCreate.clearOperationGroupId(eProcessInstances);
                break;
            case TRANSFERRED:
                //2	创建	审批人任务 APPROVER_TASK 2	自己	已撤回 RECALL 10	处理中 PROCESSING 1
                Long transferRecallTaskId = IdUtil.getSnowflakeNextId();
                taskCreate.approverTask(eProcessInstances,
                                        nodeUtils.getApproverByTask(curTask),
                                        curTask.getProcessDesignId(),
                                        TaskStatus.RECALL,
                                        transferRecallTaskId,
                                        transferRecallTaskId,
                                        null,
                                        taskGroupId,
                                        JSONObject.toJSONString(formValue),
                                        TaskActionType.OPERATION_TASK);
                //2	创建	审批人任务 APPROVER_TASK 2	下一个	已取消 CANCELED 9	处理中 PROCESSING 1
                revokeUtils.createFollowingTasksWithStatus(eProcessInstances,
                                                           curTask.getTaskGroupId(),
                                                           TaskStatus.CANCELED,
                                                           transferRecallTaskId,
                                                           formValue,
                                                           TaskActionType.LOG_TASK);
                //2	创建	审批人任务 APPROVER_TASK 2	自己	审批中 UNDER_APPROVAL 2	处理中 PROCESSING 1
                Long transferApproverTaskId = IdUtil.getSnowflakeNextId();
                taskCreate.approverTask(eProcessInstances,
                                        nodeUtils.getApproverByTask(curTask),
                                        curTask.getProcessDesignId(),
                                        TaskStatus.UNDER_APPROVAL,
                                        transferApproverTaskId,
                                        transferRecallTaskId,
                                        null,
                                        taskGroupId,
                                        JSONObject.toJSONString(formValue),
                                        TaskActionType.LOG_TASK);
                //处理中 PROCESSING 1
                eProcessInstances.setFlowStatus(FlowStatus.PROCESSING.getValue().longValue());
                updateEProcessInstances(eProcessInstances);
                //清除操作组ID
                taskCreate.clearOperationGroupId(eProcessInstances);
                break;
        }
        return curTaskId;
    }
}
